package jrain.fw.core.log;

/**
 * <pre>
 * 作者：3244924214@qq.com 
 * 描述： 日志默认实现
 * </pre>
 */
public class NoLoggingImpl implements Logger {

	private String loggerName;
	private boolean traceEnable = true;
	private boolean debugEnable = true;
	private boolean infoEnable = true;
	private boolean warnEnable = true;
	private boolean errorEnable = true;

	public NoLoggingImpl(String loggerName) {
		this.loggerName = loggerName;
	}

	public String getLoggerName() {
		return this.loggerName;
	}

	public boolean isTraceEnabled() {
		return traceEnable;
	}

	public boolean isDebugEnabled() {
		return debugEnable;
	}

	public boolean isInfoEnabled() {
		return infoEnable;
	}

	public boolean isWarnEnabled() {
		return warnEnable;
	}

	public boolean isErrorEnabled() {
		return errorEnable;
	}

	// trace-----------------------------------------------------
	public void trace(String msg) {
		if (traceEnable) {
			out(false, msg);
		}
	}

	public void trace(String format, Object... argArray) {
		if (traceEnable) {
			out(false, format, argArray);
		}
	}

	// debug-----------------------------------------------------

	public void debug(String msg) {
		if (debugEnable) {
			out(false, msg);
		}
	}

	public void debug(String format, Object... argArray) {
		if (debugEnable) {
			out(false, format, argArray);
		}
	}

	public void debug(String msg, Throwable t) {
		if (debugEnable) {
			out(false, msg, t);
		}
	}

	// info-----------------------------------------------------

	public void info(String msg) {
		if (infoEnable) {
			out(false, msg);
		}
	}

	public void info(String format, Object... argArray) {
		if (infoEnable) {
			out(false, format, argArray);
		}
	}

	public void info(String msg, Throwable t) {
		if (infoEnable) {
			out(false, msg, t);
		}
	}

	// warn-----------------------------------------------------

	public void warn(String msg) {
		if (warnEnable) {
			out(false, msg);
		}
	}

	public void warn(String format, Object... argArray) {
		if (warnEnable) {
			out(false, format, argArray);
		}
	}

	public void warn(String msg, Throwable t) {
		if (warnEnable) {
			out(false, msg, t);
		}
	}

	// error-----------------------------------------------------

	public void error(String msg) {
		if (errorEnable) {
			out(true, msg);
		}
	}

	public void error(String format, Object... argArray) {
		if (errorEnable) {
			out(true, format, argArray);
		}
	}

	public void error(String msg, Throwable t) {
		if (errorEnable) {
			out(true, msg, t);
		}
	}

	private void out(boolean error, String msg) {
		if (msg != null) {
			String nmsg = loggerName + " : " + msg;
			if (error) {
				System.err.println(nmsg);
			} else {
				System.out.println(nmsg);
			}
		}
	}

	// out-----------------------------------------------------
	private void out(boolean error, String format, Object... argArray) {
		if (format != null) {
			format = format.replace("{}", "%s");
			String msg = String.format(format, argArray);

			String nmsg = loggerName + " : " + msg;
			if (error) {
				System.err.println(nmsg);
			} else {
				System.out.println(nmsg);
			}
		}
	}

	private void out(boolean error, String msg, Throwable t) {
		if (msg != null) {
			String nmsg = loggerName + " : " + msg;
			if (error) {
				System.err.println(nmsg);
			} else {
				System.out.println(nmsg);
			}
		}
		if (t != null) {
			t.printStackTrace();
		}
	}

}
